package aftersale

import (
	"context"
	"mall/common/modelx"
	"mall/common/utils/pointy"
	"mall/service/order/model"
	"mall/service/order/rpc/internal/svc"
	"mall/service/order/rpc/types/order"
)

func GetCompanyAddress(ctx context.Context, svcCtx *svc.ServiceContext, addressId uint64) *order.CompanyAddressInfo {
	var addressInfo = &order.CompanyAddressInfo{}
	//赋值
	var b = func(res *model.CompanyAddress) {
		*addressInfo = order.CompanyAddressInfo{
			Id:             pointy.GetPointer(res.ID),
			AddressDetails: pointy.GetPointer(res.AddressDetails),
			ContactPerson:  pointy.GetPointer(res.ContactPerson),
			Mobile:         pointy.GetPointer(res.Mobile),
			IsDefault:      pointy.GetPointer(uint64(res.IsDefault)),
			Status:         pointy.GetPointer(uint64(res.Status)),
			DelStatus:      pointy.GetPointer(uint64(res.DelStatus)),
			CreatedAt:      pointy.GetPointer(modelx.FormatTime(res.CreatedAt, "")),
			UpdatedAt:      pointy.GetPointer(modelx.FormatTime(res.UpdatedAt, "")),
			DeletedAt:      pointy.GetPointer(modelx.FormatNullDotTime(res.DeletedAt, "")),
		}
	}
	if addressId > 0 {
		res, err := svcCtx.CompanyAddress.FindOneNoCache(ctx, nil, addressId)
		if err != nil {
			return nil
		}
		b(res)
		return addressInfo
	}
	filterMap := make(map[string]map[string][]any)
	filterMap[model.CompanyAddressColumns.IsDefault] = map[string][]any{
		"equalTo": {1},
	}

	addressList, _, err := svcCtx.CompanyAddress.GetList(ctx, model.M{}, -1, filterMap)
	if err != nil {
		return nil
	}
	if len(addressList) == 0 {
		return nil
	}
	b(addressList[0])
	return addressInfo
}
